﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kangjin.Web.UI.Controls.RepeaterEx
{
    [ToolboxDataAttribute("<{0}:RepeaterEx runat=\"server\"></{0}:RepeaterEx>")]
    [DesignerAttribute("System.Web.UI.Design.WebControls.RepeaterDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    public class RepeaterEx : Repeater
    {
        #region ITemplate
        private ITemplate emptyDataTemplate;
        private ITemplate itemTemplate;
        private ITemplate footerTemplate;
        private ITemplate headerTemplate;
        private ITemplate separatorTemplate;
        private ITemplate alternatingItemTemplate;

        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public virtual ITemplate EmptyDataTemplate
        {
            get { return this.emptyDataTemplate; }
            set { this.emptyDataTemplate = value; }
        }
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public new ITemplate ItemTemplate
        {
            get { return this.itemTemplate; }
            set { this.itemTemplate = value; }
        }
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public new ITemplate FooterTemplate
        {
            get { return this.footerTemplate; }
            set { this.footerTemplate = value; }
        }
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public new ITemplate HeaderTemplate
        {
            get { return this.headerTemplate; }
            set { this.headerTemplate = value; }
        }
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public new ITemplate SeparatorTemplate
        {
            get { return this.separatorTemplate; }
            set { this.separatorTemplate = value; }
        }
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [BrowsableAttribute(false)]
        [TemplateContainer(typeof(RepeaterItemEx))]
        public new ITemplate AlternatingItemTemplate
        {
            get { return this.alternatingItemTemplate; }
            set { this.alternatingItemTemplate = value; }
        }
        #endregion

        private List<RepeaterItemEx> itemsArray;
        private RepeaterItemExCollection itemsCollection;
        private int _itemCnt = -1;

        public RepeaterEx()
            : base()
        {
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new RepeaterItemExCollection Items
        {
            get
            {
                if (this.itemsCollection == null)
                {
                    if (this.itemsArray == null)
                    {
                        this.EnsureChildControls();
                    }
                    this.itemsCollection = new RepeaterItemExCollection(this.itemsArray);
                }
                return this.itemsCollection;
            }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (this.Items.Count == 0)
            {
                this.Controls.Clear();

                if (this.HeaderTemplate != null)
                {
                    this.HeaderTemplate.InstantiateIn(this);
                }

                if (this.EmptyDataTemplate != null)
                {
                    this.EmptyDataTemplate.InstantiateIn(this);
                }

                if (this.FooterTemplate != null)
                {
                    this.FooterTemplate.InstantiateIn(this);
                }
            }
        }

        protected new RepeaterItemEx CreateItem(int itemIndex, ListItemType itemType)
        {
            if(_itemCnt < 0)
            {
                IEnumerable ie = this.GetData();
                int cnt = 0;
                foreach (object e in ie){
                    cnt++;
                }
                _itemCnt = cnt;
            }
            return new RepeaterItemEx(itemIndex, itemType, _itemCnt);
        }

        protected override void CreateControlHierarchy(bool useDataSource)
        {
            IEnumerable data = null;
            int dataItemCount = -1;
            if (this.itemsArray != null)
            {
                this.itemsArray.Clear();
            }
            else
            {
                this.itemsArray = new List<RepeaterItemEx>();
            }
            if (!useDataSource)
            {
                dataItemCount = (int)this.ViewState["_!ItemCount"];
                if (dataItemCount != -1)
                {
                    data = new DummyDataSource(dataItemCount);
                    this.itemsArray.Capacity = dataItemCount;
                }
            }
            else
            {
                data = this.GetData();
                ICollection is2 = data as ICollection;
                if (is2 != null)
                {
                    this.itemsArray.Capacity = is2.Count;
                }
            }
            if (data != null)
            {
                int itemIndex = 0;
                bool flag = base.SeparatorTemplate != null;
                dataItemCount = 0;
                if (base.HeaderTemplate != null)
                {
                    this.CreateItem(-1, ListItemType.Header, useDataSource, null);
                }
                foreach (object obj2 in data)
                {
                    if (flag && (dataItemCount > 0))
                    {
                        this.CreateItem(itemIndex - 1, ListItemType.Separator, useDataSource, null);
                    }
                    ListItemType itemType = ((itemIndex % 2) == 0) ? ListItemType.Item : ListItemType.AlternatingItem;
                    RepeaterItemEx item = this.CreateItem(itemIndex, itemType, useDataSource, obj2);
                    this.itemsArray.Add(item);
                    dataItemCount++;
                    itemIndex++;
                }
                if (this.FooterTemplate != null)
                {
                    this.CreateItem(-1, ListItemType.Footer, useDataSource, null);
                }
            }
            if (useDataSource)
            {
                this.ViewState["_!ItemCount"] = (data != null) ? dataItemCount : -1;
            }
        }


        private RepeaterItemEx CreateItem(int itemIndex, ListItemType itemType, bool dataBind, object dataItem)
        {
            RepeaterItemEx item = this.CreateItem(itemIndex, itemType);
            RepeaterItemEventArgs e = new RepeaterItemEventArgs(item);
            this.InitializeItem(item);
            if (dataBind)
            {
                item.DataItem = dataItem;
            }
            this.OnItemCreated(e);
            this.Controls.Add(item);
            if (dataBind)
            {
                item.DataBind();
                this.OnItemDataBound(e);
                item.DataItem = null;
            }
            return item;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            if (this.ViewState["_!ItemCount"] != null){
                this.CreateControlHierarchy(false);
            }
            else
            {
                this.itemsArray = new List<RepeaterItemEx>();
            }
            base.ClearChildViewState();
        }

        protected void InitializeItem(RepeaterItemEx item)
        {
            ITemplate itemTemplate = null;
            switch (item.ItemType)
            {
                case ListItemType.Header:
                    itemTemplate = this.headerTemplate;
                    goto Label_005B;

                case ListItemType.Footer:
                    itemTemplate = this.footerTemplate;
                    goto Label_005B;

                case ListItemType.Item:
                    break;

                case ListItemType.AlternatingItem:
                    itemTemplate = this.alternatingItemTemplate;
                    if (itemTemplate != null)
                    {
                        goto Label_005B;
                    }
                    break;

                case ListItemType.Separator:
                    itemTemplate = this.separatorTemplate;
                    goto Label_005B;

                default:
                    goto Label_005B;
            }
            itemTemplate = this.itemTemplate;
        Label_005B:
            if (itemTemplate != null)
            {
                itemTemplate.InstantiateIn(item);
            }
        }

    }
}